<?php

namespace app\api\controller\ec;

use app\common\controller\Api;
use app\common\exception\UploadException;
use think\Config;
use think\Hook;
use think\Db;

/**
 * 公共接口
 * 第一步:先添加客户 到ec
 * 第二步:根据客户手机号 获取CrmId
 * 第三步：根据CrmId 获取客户信息
 */
class Echuichuanfeiyu extends Ecbase {
    //新入库 0  ; 产生意向 1;可跟进客户 2; 邀约上门 3; 合作客户 4;
    //    194:回访-信息确认：
    //    微信情况：同意添加微信  84728587
    //    线索阶段：产生意向、可跟进客户 1 2
    //
    //    195:回访-加为好友：
    //    微信情况：通过微信  84728574
    //
    //    196:回访-高潜成交
    //    意向程度：A类客户，B类客户 84726558  84736557
    //    线索阶段：邀约上门 4
    

    //    527:顾客到店或销售人员上门   //    
    //    线索阶段：合作客户 4
    
    // 1000
    //  电话: 空号、停机、骂人客户、酒水同行、暂停服务  84732589 84735235 84753841 84747839 84732588
    public $huifang = [
        196=>['label'=>['84726558','84736557'],'step'=>[3]],
        195=>['label'=>['84728574'],'step'=>[]],
        194=>['label'=>['84728587'],'step'=>[1,2]],
        527=>['label'=>[],'step'=>[4]],
        1000=>['label'=>['84732589','84735235','84753841','84747839','84732588'],'step'=>[]],
    ];

    public function _initialize() {
        parent::_initialize();
    }
    
    
    
    public function updateHuichuan(){
        $starttime = strtotime('yesterday');
        $endtime = $starttime + 86400;
        $today_list = db('ec_aiuser_to_ecuser_feiyu_log')->field('id,crmId,step,label')->where(['createtime'=>['between',[$starttime,$endtime]]])
                ->order('id asc')
                ->select();
      
        $huifang_finish = [];
        $huifang = [];
        foreach ($today_list as $key =>$value){
            $clue_convert_state = 0; //线索状态 ：194:回访-信息确认、195:回访-加为好友、196:回访-高潜成交
            foreach ($this->huifang as $k =>$v){
                $curent_label = explode(',', $value['label']);
                if(!empty(array_intersect($curent_label, $v['label'])) || in_array($value['step'], $v['step'])){
                    $clue_convert_state = $k;
                    break;
                }
               
            }
          
            if(empty($clue_convert_state)) continue;
            
            if($clue_convert_state == 196)$huifang_finish[] = $value['crmId'];
            
            db('ec_aiuser_to_ecuser_feiyu_log')->where('id',$value['id'])->update(['is_huichuan'=>1,'clue_convert_state'=>$clue_convert_state]);
        }
        if(!empty($huifang_finish)){
            db('ec_aiuser_to_ecuser_feiyu')->where('crmId','in',$huifang_finish)->setField('huichuan_finish',1);
        }
    }
   




    public function getKehuLable(){
       $page =  1;
       $pagesize = 200;
       $currenttime = time();
       $starttime = strtotime('today');
       $endtime = $starttime + 3600 * 24;       
       while (true){
           $list = db('ec_aiuser_to_ecuser_feiyu')
                   ->field('id,crmId')
                   ->where([
                       'huichuan_finish'=>0,
                       'createtime'=>['<',$currenttime],
                       'crmId'=>['<>','']
                    ])
                   ->limit(($page -1) * $pagesize, $pagesize)
                   ->order('id desc')->select();
           if(empty($list)) break;
           
            $crmIds = [];
            foreach ($list as $value){
                $crmIds[] = $value['crmId'];
            }
            $param['crmIds'] = implode(',', $crmIds);
            $controller = '\app\common\controller\zhongzhuan\Fenpeifeiyu';
            $item_response = call_user_func_array(array($controller, 'queryLabel'), array($param));
            if($item_response['code'] == 200 && !empty($item_response['data'])){
                $save_data = [];
                foreach ($item_response['data'] as $value){
                    $exist_info = db('ec_aiuser_to_ecuser_feiyu_log')->field('id')->where([
                        'createtime'=>['between',[$starttime,$endtime]],
                        'crmId'=>$value['crmId'],
                    ])->find();
                    if(empty($exist_info)){
                        $temp_data = [
                            'crmId'=>$value['crmId'],
                            'label'=>$value['labelIds'],
                            'createtime'=>time(),
                        ];
                        $save_data[] = $temp_data;
                    }else{
                        db('ec_aiuser_to_ecuser_feiyu_log')->where('id',$exist_info['id'])->update([
                            'label'=>$value['labelIds'],
                            'updatetime'=>time()
                        ]);
                    }                    
                }
                
                if(!empty($save_data)){
                    db('ec_aiuser_to_ecuser_feiyu_log')->insertAll($save_data);
                }
                
            }
            $page = $page + 1;
       }
       
   }
    
    
   public function getKehuStep(){
       $page =  1;
       $pagesize = 10;
       $currenttime = time();
       $starttime = strtotime('today');
       $endtime = $starttime + 3600 * 24;
       while (true){
           $list = db('ec_aiuser_to_ecuser_feiyu')
                   ->field('id,crmId')
                   ->where([
                       'huichuan_finish'=>0,
                       'createtime'=>['<',$currenttime],
                       'crmId'=>['<>','']
                    ])
                   ->limit(($page -1) * $pagesize, $pagesize)
                   ->order('id desc')->select();
           if(empty($list)) break;
           
            $crmIds = [];
            foreach ($list as $value){
                $crmIds[] = $value['crmId'];
            }
            $param['crmIds'] = $crmIds;
            $controller = '\app\common\controller\zhongzhuan\Fenpeifeiyu';
            $item_response = call_user_func_array(array($controller, 'getKehuInfoByCrmId'), array($param));
            qqq('getKehuStep',$item_response);
            if($item_response['code'] == 200 && !empty($item_response['data'])){
                $save_data = [];
                foreach ($item_response['data']['list'] as $value){
                    $exist_info = db('ec_aiuser_to_ecuser_feiyu_log')->field('id')->where([
                        'createtime'=>['between',[$starttime,$endtime]],
                        'crmId'=>$value['crmId'],
                    ])->find();
                    if(empty($exist_info)){
                        $temp_data = [
                            'crmId'=>$value['crmId'],
                            'step'=> $value['step'],
                            'createtime'=>time(),
                        ];
                        $save_data[] = $temp_data;
                    }else{
                        db('ec_aiuser_to_ecuser_feiyu_log')->where('id',$exist_info['id'])->update([
                            'step'=> $value['step'],
                            'updatetime'=>time()
                        ]);
                    }                    
                }
                
                if(!empty($save_data)){
                    db('ec_aiuser_to_ecuser_feiyu_log')->insertAll($save_data);
                }
                
            }
            $page = $page + 1;
       }
       
       
   }
   
   
   
   
    
    public function getStages(){
        $controller = '\app\common\controller\zhongzhuan\Fenpeifeiyu';
        $item_response = call_user_func_array(array($controller, 'getStages'),[]);
      
    }

    



    public function getLabelConfig(){
        $list = db('ec_label')->where('labelGroupName','in',['微信情况','意向程度'])->select();
        $end_list = [];
        foreach ($list as $value){
            $labelList = json_decode($value['labelList'],true);
            
            foreach ($labelList as $v){
                
                $end_list[$value['labelGroupName']][$v['labelName']] = $v['labelId'];
            }
            
            
        }
        return $end_list;
    }
   
   
    


   
    
  
}
